/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.services.impl;

import cz.muni.fi.pa165.bookingmanager.dao.AdministratorManager;
import cz.muni.fi.pa165.bookingmanager.entities.Administrator;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import cz.muni.fi.pa165.bookingmanager.interceptors.LoggingInterceptor;
import cz.muni.fi.pa165.bookingmanager.interceptors.ShiroSecuredInterceptor;
import cz.muni.fi.pa165.bookingmanager.services.AdministratorService;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.SessionContext;
import javax.interceptor.Interceptors;
import org.apache.shiro.authz.annotation.*;

/**
 *
 * @author Radek Koubsky
 */
@Stateless
@Local(value = AdministratorService.class)
public class AdministratorServiceImpl implements AdministratorService {

    @Resource
    private SessionContext ctx;
    @EJB
    private AdministratorManager adminManager;

    @Override
    @Interceptors({ShiroSecuredInterceptor.class, LoggingInterceptor.class})
    @RequiresRoles("superadmin")
    public void createAdministrator(Administrator administrator) throws IllegalOrphanException, PreexistingEntityException {
        if (!isValidEntity(administrator)) {
            throw new IllegalArgumentException("Invalid administrator entity.");
        }

     
            adminManager.createAdministrator(administrator);
        
       
    }

    @Override
    @Interceptors({ShiroSecuredInterceptor.class, LoggingInterceptor.class})
    @RequiresRoles("superadmin")
    public void updateAdministrator(Administrator administrator) throws NonexistentEntityException, IllegalOrphanException, PreexistingEntityException, Exception {
        if (!isValidEntity(administrator)) {
            throw new IllegalArgumentException("Invalid administrator entity.");
        }

        try {
            adminManager.updateAdministrator(administrator);
        } catch (Exception ex) {
            Logger.getLogger(AdministratorServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            ctx.setRollbackOnly();
        }
    }

    @Override
    @Interceptors({ShiroSecuredInterceptor.class, LoggingInterceptor.class})
    @RequiresRoles("superadmin")
    public void deleteAdministrator(Administrator administrator) throws NonexistentEntityException, IllegalOrphanException {
        try {
            adminManager.deleteAdministrator(administrator);
        } catch (Exception ex) {
            Logger.getLogger(AdministratorServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            ctx.setRollbackOnly();
        }
    }

    @Override
    @Interceptors({ShiroSecuredInterceptor.class, LoggingInterceptor.class})
    @RequiresRoles("superadmin")
    public Administrator getAdministratorById(Long administratorId) {
        return adminManager.getAdministratorById(administratorId);
    }   

    @Override
    @Interceptors({ShiroSecuredInterceptor.class, LoggingInterceptor.class})
    @RequiresRoles("superadmin")
    public List<Administrator> findAllAdministrators() {
        List<Administrator> administrators = adminManager.findAllAdministrators();
        return administrators;
    }
    
    @Override
    @Interceptors(LoggingInterceptor.class)
    public Administrator getAdministratorByUserName(String username){
            return adminManager.getAdministratorByUserName(username);
    }
    
    private boolean isValidEntity(Administrator administrator) {
        if (administrator == null) {
            return false;
        }
        if (administrator.getFirstName() == null) {
            return false;
        }

        if (administrator.getSurName() == null) {
            return false;
        }

        if (administrator.getUsername() == null) {
            return false;
        }
        
        if (administrator.getPassword() == null) {
            return false;
        }

        return true;
    }

    public void setAdministratorManager(AdministratorManager cusManager) {
        this.adminManager = cusManager;
    }
    
       
}
